package com.example.pre.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.pre.business.DepartmentBusiness;
import com.example.pre.controller.base.ControllerBase;
import com.example.pre.exception.ExceptionMessage;
import com.example.pre.exception.ExtParaException;
import com.example.pre.exception.ServiceException;
import com.example.pre.model.Department;
import com.example.pre.model.vo.DepartmentVO;
import com.example.pre.tools.StringUtils;
import com.example.pre.tools.Tools;

/**
 * 部门controller层接口
 * 
 * @author Administrator
 *
 */
@RestController
public class DepartmentController extends ControllerBase {

	private final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private DepartmentBusiness departmentBusiness;

	/**
	 * 创建部门
	 * 
	 * @param name        部门名称
	 * @param description 部门描述
	 * @return flag,message,code,data-部门实体 ，page：分页信息
	 */
	@RequestMapping(value = "/department", method = RequestMethod.POST)
	public Map<String, Object> createDepartment(@RequestParam String name,
			@RequestParam(required = false) String description) {

		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = ",传入的参数name=" + name + ",description=" + description;
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);
		result.put("data", null);
		try {
			if (StringUtils.isEmpty(name)) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "部门名不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}
			Department department = departmentBusiness.createDepartment(name, description);
			result.put("data", department);
			// 存返回数据
			message = "创建部门成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "创建部门时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "创建部门时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "创建部门时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}

	}

	/**
	 * 获取部门下所有用户
	 * 
	 * @param departmentId 部门id
	 * @return flag,message,code,data-部门VO实体 ，page：分页信息
	 */
	@RequestMapping(value = "/department/user", method = RequestMethod.GET)
	public Map<String, Object> getDepartmentUsers(@RequestParam Integer departmentId) {

		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = ",传入的参数departmentId=" + departmentId;
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);
		result.put("data", null);
		try {
			if (departmentId == null || departmentId < 0) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "部门id不能为空或小于0";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}

			DepartmentVO users = departmentBusiness.getDepartmentUsers(departmentId);
			result.put("data", users);
			// 存返回数据
			message = "获取部门下用户成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "获取部门下用户时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "获取部门下用户时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "获取部门下用户时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}

	}

	/**
	 * 获取部门列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/department", method = RequestMethod.GET)
	public Map<String, Object> getDepartments() {

		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = null;
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);
		result.put("data", null);
		try {

			List<Department> departments = departmentBusiness.getDepartments();
			result.put("data", departments);
			// 存返回数据
			message = "获取部门列表成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "获取部门列表时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "获取部门列表时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "获取部门列表时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}

	}

}
